const md = require('../../utils/md5.js');
const app = getApp()
var _paystate =0
const post = require('../../utils/post.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    model: 1,
    memo:'',
    array:{name:'采购货款',id:0},
    index:0,
    typename:"PU",
    tranname:"采购货款"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.id) {
      this.setData({
        tmebid: options.id,
        imgurl: options.imgurl,
        mebname: options.mebname,
        status: options.state,
        fraccid: options.fraccid
      })
    }
    this.typelist();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  cancer:function(){
    wx.navigateBack({
      delta:1
    })
  },
  getmemo:function(e){
    this.setData({
      memo: e.detail.value
    })
  },
  typelist: function () {
    var that = this;
    var datas = {};
    datas.where = "IsPayType = 1";
    var datacall = function (res) {
      console.log(res);
      if(res.data.code == 0){
        let arraylist = [];
        let ll={};
        for(let i=0;i<res.data.data.length;i++){
          ll.name = res.data.data[i].TranName;
          ll.id = res.data.data[i].TranType;
          arraylist.push(ll);
          ll={};
        }
        that.setData({
          array:arraylist,
          tranname:arraylist[0].name,
          typename: arraylist[0].id,
        })
        console.log(arraylist)
      }
    }
    var res2 = post.getdata("/BO/TR_TranType/Query", datas, datacall);
  },
  bindPickerChange: function (e) {
    var that = this;
    console.log(that.data.array[e.detail.value])
    let trantype = that.data.array[e.detail.value];
    that.setData({
      index: e.detail.value,
      typename:trantype.id,
      tranname:trantype.name
    })
  },
  formSubmit: function (e) {
  
    var that = this;
    var money = e.detail.value.money;
    money = parseFloat(money);
    if (money.length == 0) {
      wx.showModal({
        title: '',
        content: '转账金额不能为空',
      })
      return;
    }
    that.setData({
      model: 0,
      money: money
    })
    return;
  },
  makesurepwd: function () {
    var that = this;
    if (_paystate==1){
      // console.log(45464564)
      return;
    }
    _paystate = 1;
    that.setData({
      paystate:1
    })
    that.setData({
      paying:1
    })
    if (that.data.pwd==''){
      wx.showModal({
        title: '',
        content: '支付密码不能为空！',
      })
      that.setData({
        paying: 0
      })
      return;
    }
    wx.showLoading({
      title: '正在付款...',
    })
    var apinameIsMeb = "/Action/ZKPay/ExecPayment01";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        PayMebID: wx.getStorageSync('mebid'),
        PayAccID: wx.getStorageSync('depid'),
        GetAccID: that.data.fraccid,
        GetMebID: that.data.tmebid,
        Money: that.data.money,
        PayDetail: that.data.memo,
        PayPsw: md.hexMD5(that.data.pwd),
        TranType:that.data.typename
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        wx.hideLoading();
        _paystate= 0
        console.log(res);
        if (res.data.code == 0) {
          wx.redirectTo({
            url: '/pages/success/success?name=' + res.data.data.MebName + "&photo=" + res.data.data.PhotoUrl + "&money=" + that.data.money + "&paytype=支付" + "&state=" + res.data.data._BOState,
          })
        }else{
          wx.showModal({
            title: '',
            content: res.data.msg,
            success:function(res){
              if (res.confirm) {
                wx.navigateBack({
                  delta:1
                })
              } else {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }
      },
      fail:function(res){
        wx.hideLoading();
        console.log(res);
      }
    })
  },
  getpwd: function (e) {
    this.setData({
      pwd: e.detail.value
    })
  }
})